package gui.Teacher;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import control.AddQuestionControl;
import control.RemoveQuestionControl;
import BaseClasses.Question;
import BaseClasses.Subject;
import System.MainControl;
import System.SysMode;
import System.SystemObject;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.Font;
import java.awt.SystemColor;
import java.util.List;

public class AddQuestionGui extends JPanel implements ActionListener ,KeyListener{
	
	//**Class Variables*//
	private static final long serialVersionUID = 4785671943950308907L;
	
	public JButton btnAddQuestion;
	public JButton btnBack;
	private JTextField textField_4;
	private JTextField textField_5;
	private JTextField textField_6;
	private JTextField textField_7;
	private JLabel lblChooseSubject;		
	private JTextField textField_8;
	private AddQuestionControl control;
	private JTextField textField_9;
	private Question q;
	private JTextField textField_2;
	private JComboBox comboBox;
	private JTextField txtEnterQuestionidStarted;
	
	public AddQuestionGui() {
	    super();
	    setBackground(SystemColor.activeCaption);
	    control = new AddQuestionControl();
	    initialize();
}

	/**
	 * Set Panel properties & putting components on panel
	 */
	private void initialize() {
	
	this.setSize(500, 500);
	this.setLayout(null);
	setBorder(new TitledBorder(null, "Add Question", TitledBorder.LEADING, TitledBorder.TOP, null, null));
	lblChooseSubject = new JLabel("Choose Subject");
	lblChooseSubject.setHorizontalAlignment(SwingConstants.CENTER);
	lblChooseSubject.setFont(new Font("Tahoma", Font.BOLD, 14));
	lblChooseSubject.setBounds(32, 24, 121, 22);
	add(lblChooseSubject);
	
	JLabel lblSetQuetionId = new JLabel("Set Quetion ID");
	lblSetQuetionId.setHorizontalAlignment(SwingConstants.CENTER);
	lblSetQuetionId.setFont(new Font("Microsoft New Tai Lue", Font.BOLD, 14));
	lblSetQuetionId.setBounds(32, 116, 104, 14);
	add(lblSetQuetionId);
	
	add(getTextQuestionId());
	
	JLabel lblQuestion = new JLabel("Question");
	lblQuestion.setHorizontalAlignment(SwingConstants.CENTER);
	lblQuestion.setFont(new Font("Microsoft New Tai Lue", Font.BOLD, 14));
	lblQuestion.setBounds(32, 156, 75, 14);
	add(lblQuestion);
	
	JLabel lblAnswers = new JLabel("Answers:");
	lblAnswers.setHorizontalAlignment(SwingConstants.CENTER);
	lblAnswers.setFont(new Font("Microsoft New Tai Lue", Font.BOLD, 14));
	lblAnswers.setBounds(32, 192, 65, 14);
	add(lblAnswers);
	
	JLabel label = new JLabel("1");
	label.setFont(new Font("Tahoma", Font.BOLD, 14));
	label.setBounds(157, 190, 17, 14);
	add(label);
	
	textField_4 = new JTextField();
	textField_4.setBounds(189, 189, 86, 20);
	add(textField_4);
	textField_4.setColumns(10);
	
	JLabel label_1 = new JLabel("2");
	label_1.setHorizontalAlignment(SwingConstants.CENTER);
	label_1.setFont(new Font("Tahoma", Font.BOLD, 14));
	label_1.setBounds(146, 225, 28, 14);
	add(label_1);
	
	textField_5 = new JTextField();
	textField_5.setBounds(189, 220, 86, 20);
	add(textField_5);
	textField_5.setColumns(10);
	
	JLabel label_2 = new JLabel("3");
	label_2.setHorizontalAlignment(SwingConstants.CENTER);
	label_2.setFont(new Font("Tahoma", Font.BOLD, 14));
	label_2.setBounds(146, 262, 28, 14);
	add(label_2);
	
	textField_6 = new JTextField();
	textField_6.setBounds(189, 261, 86, 20);
	add(textField_6);
	textField_6.setColumns(10);
	
	JLabel label_3 = new JLabel("4");
	label_3.setFont(new Font("Tahoma", Font.BOLD, 14));
	label_3.setBounds(157, 305, 17, 14);
	add(label_3);
	
	textField_7 = new JTextField();
	textField_7.setBounds(189, 304, 86, 20);
	add(textField_7);
	textField_7.setColumns(10);
	
	JLabel lblChooseCorrectAnswer = new JLabel("Enter Correct Answer");
	lblChooseCorrectAnswer.setFont(new Font("Microsoft New Tai Lue", Font.BOLD, 13));
	lblChooseCorrectAnswer.setBounds(25, 343, 149, 14);
	add(lblChooseCorrectAnswer);
	
	btnAddQuestion= new JButton("Add Question");
	btnAddQuestion.setFont(new Font("Microsoft New Tai Lue", Font.BOLD, 14));
	btnAddQuestion.addActionListener(this);
	btnAddQuestion.setBounds(66, 394, 127, 23);
	add(btnAddQuestion);
	
	btnBack = new JButton("Back");
	btnBack.setFont(new Font("Microsoft New Tai Lue", Font.BOLD, 14));
	btnBack.addActionListener(this);
	btnBack.setBounds(256, 394, 89, 23);
	add(btnBack);
	
	textField_8 = new JTextField();
	textField_8.setBounds(185, 148, 160, 30);
	add(textField_8);
	textField_8.setColumns(10);
	
	textField_9 = new JTextField();
	textField_9.setColumns(10);
	textField_9.setBounds(189, 340, 86, 20);
	add(textField_9);
	
	comboBox = new JComboBox();
	comboBox.setBounds(185, 27, 104, 20);
	SystemObject sys = new SystemObject(null, SysMode.getSubject);
	control.sendToServer(sys);
	
	List<Subject> subjectList = null;
	SystemObject retObj = control.GetSysObj();
	if (retObj.getMode() == SysMode.OK)
		subjectList = (List<Subject>)retObj.getObj();
	
	if (subjectList != null)
	{
		for (int i=0;i<subjectList.size();i++)
		{
			comboBox.addItem(subjectList.get(i).getCode());
		}
	}
		add(comboBox);
		
		txtEnterQuestionidStarted = new JTextField();
		txtEnterQuestionidStarted.setEditable(false);
		txtEnterQuestionidStarted.setBackground(SystemColor.activeCaption);
		txtEnterQuestionidStarted.setFont(new Font("Microsoft New Tai Lue", Font.BOLD, 14));
		txtEnterQuestionidStarted.setText("Enter QuestionID started with subject ID:");
		txtEnterQuestionidStarted.setBounds(32, 70, 268, 20);
		add(txtEnterQuestionidStarted);
		txtEnterQuestionidStarted.setColumns(10);

}
	
	
	private JTextField getTextQuestionId()
	{
		if(textField_2 == null)
		{
			textField_2 = new JFormattedTextField();
			textField_2.setColumns(10);
			textField_2.setBounds(189, 113, 86, 20);
			textField_2.addKeyListener(this);
		}
		return textField_2;
	} 
	
	/**
	 * Checking The QuestionId is OK 
	 * @return Mode - OK or ERROR
	 */
/*	private SysMode InputCheck()
	{
	inptCheck=getTextQuestionId().getText();
	for(int i=0;i<inptCheck.length();i++)
		if(!(Character.isDigit(inptCheck.charAt(i))))
			return SysMode.Input_CHAR_ERR;
		else
			if(inptCheck.length()<0 || inptCheck.length()<5)
			return SysMode.Input_LENGTH_ERR;
	return SysMode.OK;
	}
	*/

	/**
	 * Checking The Number OF points is OK 
	 * @return Mode - OK or ERROR
	 */
/*	private SysMode NumberOfPtsCheck()
	{
	nopCheck=getTextNumberOfpts().getText();
	for(int i=0;i<nopCheck.length();i++)
		if(!(Character.isDigit(nopCheck.charAt(i))))
			return SysMode.Input_CHAR_ERR;
		else
			if(nopCheck.length()<0 || nopCheck.length()>100)
			return SysMode.Input_LENGTH_ERR;
	return SysMode.OK;
	}
	*/
@Override			
public void actionPerformed(ActionEvent e) 
{

	if(e.getSource() == btnBack)
	{
		
		MainControl.setGUI(MainControl.currentUser.getRole(), this);
	}
	if(e.getSource()==btnAddQuestion)
	{
		String sub=(String)comboBox.getSelectedItem();
		
		q = new Question(textField_2.getText(),textField_8.getText(),textField_4.getText(),textField_5.getText(),textField_6.getText(),textField_7.getText(),textField_9.getText(),sub);
			control = new AddQuestionControl();
			
			SystemObject sys = new SystemObject(q, SysMode.AddQuestion);
			control.sendToServer(sys);
			SystemObject retObj = control.GetSysObj();
				if (retObj.getMode() == SysMode.OK)
				{
				JOptionPane.showMessageDialog(null,"Insert Success","System Message",JOptionPane.INFORMATION_MESSAGE);
				MainControl.setGUI(MainControl.currentUser.getRole(), this);
				}
		}
	}

	


@Override
public void keyPressed(KeyEvent EVT) {
	// TODO Auto-generated method stub

}
@Override
public void keyReleased(KeyEvent arg0) {
	// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent EVT) {
	// TODO Auto-generated method stub

}
}


